package com.hoyue.servlet;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hoyue.bean.Introduction;
import com.hoyue.service.IntroductionService;

@WebServlet("/IntroductionServlet")
public class IntroductionServlet extends HttpServlet {

    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("method");//获取方法名
        if(name == null || name.isEmpty()){
            throw new RuntimeException("没有传递method参数,请给出你想调用的方法");
        }
        Class c = this.getClass();//获得当前类的Class对象
        Method method = null;
        try {
            //获得Method对象
            method = getClass().getDeclaredMethod(name, HttpServletRequest.class,HttpServletResponse.class);
        } catch (Exception e) {
            throw new RuntimeException("没有找到"+name+"方法，请检查该方法是否存在");
        }

        try {
            method.invoke(this, request,response);//反射调用方法
        } catch (Exception e) {
            System.out.println("你调用的方法"+name+",内部发生了异常");
            throw new RuntimeException(e);
        }
    }

    /*前台颜面获取数据库信息*/
    protected void getIntroduction(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        IntroductionService introductionService = new IntroductionService();
        List<Introduction> introductions = introductionService.getAllIntro();
        request.setAttribute("introduction", introductions);
        request.getRequestDispatcher("index.jsp").forward(request, response);

    }

    /*后台页面获取数据库信息*/
    protected void getAdminIntroduction(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        IntroductionService introductionService = new IntroductionService();
        List<Introduction> introductions = introductionService.getAllIntro();
        request.setAttribute("introduction", introductions);
        request.getRequestDispatcher("/WEB-INF/admin/about_hoyue.jsp").forward(request, response);

    }

    /*后台修改数据库信息*/
    protected void updataAdminIntroduction(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            int id = Integer.parseInt(request.getParameter("id"));
            String title = request.getParameter("title");
            String content = request.getParameter("content");
            IntroductionService introductionService = new IntroductionService();
            Introduction data = new Introduction();
            data.setId(id);
            data.setTitle(title);
            data.setContent(content);
            introductionService.updateIntro(data);
            request.getRequestDispatcher("/IntroductionServlet?method=getAdminIntroduction").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
